﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PlutoStudio
{
    /// <summary>
    /// 
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        ///     判断指定类型是否为数值类型
        /// </summary>
        /// <param name="type">要检查的类型</param>
        /// <returns>是否是数值类型</returns>
        public static bool IsNumeric(this Type type)
        {
            return type == typeof(byte)
                || type == typeof(short)
                || type == typeof(int)
                || type == typeof(long)
                || type == typeof(sbyte)
                || type == typeof(ushort)
                || type == typeof(uint)
                || type == typeof(ulong)
                || type == typeof(decimal)
                || type == typeof(double)
                || type == typeof(float);
        }



        /// <summary>
        ///  获取成员元数据的Description特性描述信息
        /// </summary>
        /// <param name="member">成员元数据对象</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DescriptionAttribute"/> 特性描述信息，如不存在则返回 null</returns>
        public static string GetDescription(this MemberInfo member, bool inherit = false)
        {
            TypeDescriptor t;
            DescriptionAttribute desc = member.GetAttribute<DescriptionAttribute>(inherit);
            return desc == null ? null : desc.Description;
        }

        /// <summary>
        ///  获取类型元数据的Description特性描述信息
        /// </summary>
        /// <param name="type">类型元数据对象</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DescriptionAttribute"/> 特性描述信息，如不存在则返回 null</returns>
        public static string GetDescription(this Type type, bool inherit = false)
        {
            TypeDescriptor t;
            DescriptionAttribute desc = type.GetAttribute<DescriptionAttribute>(inherit);
            return desc == null ? null : desc.Description;
        }

        /// <summary>
        ///  获取类型元数据的Description特性描述信息
        /// </summary>
        /// <param name="object">类型元数据对象</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DescriptionAttribute"/> 特性描述信息，如不存在则返回 null</returns>
        public static string GetDescription<TModel>(this TModel @object, bool inherit = false)
        {
            return typeof(TModel).GetDescription(inherit);
        }

        /// <summary>
        ///  获取成员元数据的 <see cref="DescriptionAttribute"/> 特性描述信息
        /// </summary>
        /// <param name="object">成员元数据对象</param>
        /// <param name="propertyExpression">属性</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DescriptionAttribute"/> 特性描述信息，如不存在则返回 null</returns>
        public static string GetDescription<TModel, TProperty>(this TModel @object, Expression<Func<TModel, TProperty>> propertyExpression, bool inherit = false)
        {
            MemberInfo member = ((dynamic)propertyExpression.Body).Member;
            return member.GetDescription();
        }


        /// <summary>
        ///  获取成员元数据的 <see cref="DisplayNameAttribute"/> 特性描述信息
        /// </summary>
        /// <param name="member">成员元数据对象</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DisplayNameAttribute"/> 特性描述信息，如不存在则返回成员的名称</returns>
        public static string GetDisplayName(this MemberInfo member, bool inherit = false)
        {
            var desc = member.GetAttribute<DisplayNameAttribute>(inherit);
            return desc == null ? member.Name : desc.DisplayName;
        }

        /// <summary>
        ///  获取类型元数据的 <see cref="DisplayNameAttribute"/> 特性描述信息
        /// </summary>
        /// <param name="type">类型元数据对象</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DisplayNameAttribute"/> 特性描述信息，如不存在则返回成员的名称</returns>
        public static string GetDisplayName(this Type type, bool inherit = false)
        {
            var desc = type.GetAttribute<DisplayNameAttribute>(inherit);
            return desc == null ? type.Name : desc.DisplayName;
        }

        /// <summary>
        ///  获取类型元数据的 <see cref="DisplayNameAttribute"/> 特性描述信息
        /// </summary>
        /// <param name="object">类型元数据对象</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DisplayNameAttribute"/> 特性描述信息，如不存在则返回成员的名称</returns>
        public static string GetDisplayName<TModel>(this TModel @object, bool inherit = false)
        {
            return typeof(TModel).GetDisplayName(inherit);
        }

        /// <summary>
        ///  获取成员元数据的 <see cref="DisplayNameAttribute"/> 特性描述信息
        /// </summary>
        /// <param name="object">成员元数据对象</param>
        /// <param name="propertyExpression">属性</param>
        /// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
        /// <returns>返回 <see cref="DisplayNameAttribute"/> 特性描述信息，如不存在则返回成员的名称</returns>
        public static string GetDisplayName<TModel, TProperty>(this TModel @object, Expression<Func<TModel, TProperty>> propertyExpression, bool inherit = false)
        {
            MemberInfo member = ((dynamic)propertyExpression.Body).Member;
            return member.GetDisplayName(inherit);
        }



        /// <summary>
        /// 检查指定指定类型中是否存在指定的Attribute特性
        /// </summary>
        /// <typeparam name="T">要检查的Attribute特性类型</typeparam>
        /// <param name="type">要检查的类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>是否存在</returns>
        public static bool AttributeExists<T>(this Type type, bool inherit) where T : Attribute
        {
            return type.GetAttribute<T>(inherit) != null;
        }

        /// <summary>
        /// 从类型成员获取指定Attribute特性
        /// </summary>
        /// <typeparam name="TAttribute">Attribute特性类型</typeparam>
        /// <param name="type">类型类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个，不存在返回null</returns>
        public static TAttribute GetAttribute<TAttribute>(this Type type, bool inherit = false) where TAttribute : Attribute
        {
            return type.GetAttributes<TAttribute>(inherit).FirstOrDefault();
        }
        /// <summary>
        /// 从类型成员获取指定Attribute特性
        /// </summary>
        /// <typeparam name="TAttribute">Attribute特性类型</typeparam>
        /// <param name="type">类型类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个，不存在返回null</returns>
        public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this Type type, bool inherit = false) where TAttribute : Attribute
        {
            return type.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>();
        }


        /// <summary>
        /// 检查指定指定类型成员中是否存在指定的Attribute特性
        /// </summary>
        /// <typeparam name="TAttribute">要检查的Attribute特性类型</typeparam>
        /// <param name="memberInfo">要检查的类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>是否存在</returns>
        public static bool AttributeExists<TAttribute>(this MemberInfo memberInfo, bool inherit = false) where TAttribute : Attribute
        {
            return memberInfo.GetAttribute<TAttribute>(inherit) != null;
        }

        /// <summary>
        /// 从类型成员获取指定Attribute特性
        /// </summary>
        /// <typeparam name="TAttribute">Attribute特性类型</typeparam>
        /// <param name="memberInfo">类型类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个，不存在返回null</returns>
        public static TAttribute GetAttribute<TAttribute>(this MemberInfo memberInfo, bool inherit = false) where TAttribute : Attribute
        {
            return memberInfo.GetAttributes<TAttribute>(inherit).FirstOrDefault();
        }

        /// <summary>
        /// 从类型成员获取指定Attribute特性
        /// </summary>
        /// <typeparam name="TAttribute">Attribute特性类型</typeparam>
        /// <param name="memberInfo">类型类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个，不存在返回null</returns>
        public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this MemberInfo memberInfo, bool inherit = false) where TAttribute : Attribute
        {
            return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>();
        }

        /// <summary>
        /// 判断当前类型的对象能分配于指定泛型类型
        /// </summary>
        /// <param name="givenType">给定类型</param>
        /// <param name="genericType">泛型类型</param>
        /// <returns></returns>
        public static bool IsAssignableToGenericType(this Type givenType, Type genericType)
        {
            if (!genericType.IsGenericType)
            {
                return false;
            }
         var interfaceTypes = givenType.GetInterfaces();
            if (interfaceTypes.Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == genericType))
            {
                return true;
            }
            if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
            {
                return true;
            }
            Type baseType = givenType.BaseType;
            if (baseType == null)
            {
                return false;
            }
            return IsAssignableToGenericType(baseType, genericType);
        }

        /// <summary>
        /// 将字符串转换成真假
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ToBool(this object obj)
        {
            if (obj != null && obj.ToString().Equals("true", StringComparison.CurrentCultureIgnoreCase))
                return true;
            return false;
        }

        /// <summary>
        /// 将字符串转换成真假
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ToBool(this string obj)
        {
            if (obj != null && obj.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                return true;
            return false;
        }
        /// <summary>
        /// 将数值转换成真假
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ToBool(this int value)
        {
            return value != 0;
        }
        /// <summary>
        /// 将数值转换成真假
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ToBool(this int? value)
        {
            return value.GetValueOrDefault(0) != 0;
        }
    }

}
